package com.superhelper.task.job.order;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.ele.AccessToken;
import com.superhelper.common.domain.ele.ElePushEntity;
import com.superhelper.common.service.AliMQProducerService;
import com.superhelper.common.utils.JsonHelper;
import com.superhelper.common.utils.SpringContextUtil;
import com.superhelper.task.job.ShopCache;

import eleme.openapi.sdk.api.entity.message.OMessage;
import eleme.openapi.sdk.api.exception.ServiceException;
import eleme.openapi.sdk.api.service.MessageService;
import eleme.openapi.sdk.config.Config;
import eleme.openapi.sdk.oauth.response.Token;

public class EleUnPushOrderJob implements SimpleJob {
    private static final Logger log = LoggerFactory.getLogger(EleUnPushOrderJob.class);
    @Autowired
    private AliMQProducerService producer;

    @Value("${ele.secret}")
    private String secret;

    @Value("${ele.key}")
    private String key;

    @Value("${ele.appId}")
    private Integer eleAppId;

    private Config config;

    @Autowired
    private ShopCache shopCache;

    @Override
    public void execute(ShardingContext shardingContext) {
        if (config == null) {
            config = new Config(SpringContextUtil.test(), key, secret);
        }
        for (AuserShop shop : shopCache.getAllShop()) {
            if (!"ele".equals(shop.getShop_type())) {
                continue;
            }
            AccessToken accessToken = JsonHelper.fromJson(shop.getToken(), AccessToken.class);

            Token token = new Token();
            token.setAccessToken(accessToken.getToken());
            token.setExpires(accessToken.getExpires());
            token.setRefreshToken(accessToken.getRefreshToken());
            token.setTokenType(accessToken.getTokenType());
            MessageService messageService = new MessageService(config, token);
            try {
                List<OMessage> messages = messageService.getNonReachedOMessages(eleAppId);
                log.info("shopId:{}, unPushSize:{}", shop.getShopId(), messages == null ? 0 : messages.size());
                if (messages == null) {
                    continue;
                }
                for (OMessage m : messages) {
                    ElePushEntity e = new ElePushEntity();
                    e.setMessage(m.getMessage());
                    e.setRequestId(m.getRequestId());
                    e.setShopId(m.getShopId());
                    e.setTimestamp(m.getTimestamp());
                    e.setType(m.getType());
                    e.setUserId(m.getUserId());
                    producer.sendEleOrder(e);
                }
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        }
    }
}
